package com.itheima.sevrice.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.bean.ShoppingCart;
import com.itheima.bean.User;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.sevrice.ShoppingCartService;
import com.itheima.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired(required = false)
    private ShoppingCartDao shoppingCartDao;

    @Autowired
    HttpSession session;
    @Autowired
    @Lazy
    private ShoppingCartService shoppingCartService;

     /**
     * 加入购物车添加菜品 或 套餐
     *
     * @param shoppingCart
     * @return
     */
   /* @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
       LambdaUpdateWrapper<ShoppingCart> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        if (shoppingCart.getDishId()!=null){
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCartNew = shoppingCartDao.selectOne(lqw);

        if (shoppingCartNew!=null){
            //购物车里面有数据
            shoppingCartNew.setNumber(shoppingCartNew.getNumber()+1);
            shoppingCartDao.updateById(shoppingCartNew);
            return shoppingCartNew;
        }
        //第一次添加 ,需要设置 用户的id ，添加的时间  数量
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setNumber(1);
         shoppingCartDao.insert(shoppingCart);
         return shoppingCart;
    }*/


     /** 加入购物车添加菜品 或 套餐 。
     * 1. 需要判定购物车里面是否有这一道菜或套餐。
     *  1.1如果没有这个菜或套餐 直接添加即可
     *  1.2如果有这个菜或套餐，就要改变数量+1，执行更新操作
     *  1.3处理使用菜品id 或套餐id 来查询外 ，还有加上用户的id 。不然不知道是谁的订单
     *
     *
     * @param shoppingCart
     * @return*/
    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        //构建条件
        LambdaUpdateWrapper<ShoppingCart> lqw = new LambdaUpdateWrapper<>();
        //追加条件 ，查询这个用户的购物车
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //追加条件 判断用户 添加的是菜品 还是套餐
        if (shoppingCart.getDishId()!=null){
            //用户添加的是菜品
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            //用户添加的是套餐
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //执行条件
        ShoppingCart selectOne = shoppingCartDao.selectOne(lqw);

        //判断是否存在数据
        if (selectOne!=null){
            //表明数据库里面 有这件菜品|套餐 ,数据需要+1
            selectOne.setNumber(selectOne.getNumber()+1);
            //更新到数据库
            shoppingCartDao.updateById(selectOne);
            return selectOne;
        }
        //表明数据库里面没有这件菜品 |套餐
        //填充数据
        //设置用户的id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //设置添加时间
        shoppingCart.setCreateTime(LocalDateTime.now());
        //设置数据
        shoppingCart.setNumber(1);

        //添加到数据库
        shoppingCartDao.insert(shoppingCart);
        return shoppingCart;
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> findShoppingCart() {
        User user = (User) session.getAttribute("user");
        LambdaUpdateWrapper<ShoppingCart> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(ShoppingCart::getUserId, user.getId());
        return shoppingCartDao.selectList(lqw);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @Override
    public int delete() {
        LambdaUpdateWrapper<ShoppingCart> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        return shoppingCartDao.delete(lqw);
    }

    /**
     * 根据订单id 清空购物车
     * @param id
     * @return
     */
    @Override
    public int remove(Long id) {
        LambdaUpdateWrapper<ShoppingCart> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(ShoppingCart::getId,id);
        return shoppingCartDao.delete(lqw);
    }

    /**
     * 减少一件菜品|套餐
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart sub(ShoppingCart shoppingCart) {
        //得到菜品的id
        Long dishId = shoppingCart.getDishId();
        LambdaUpdateWrapper<ShoppingCart> lqw = new LambdaUpdateWrapper<>();
        //查询该用户的购物车数据
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //判断是菜品 还是 套餐
        if (dishId!=null){
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else {
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查出 这个用户的 菜品|套餐
        ShoppingCart cartOne = shoppingCartDao.selectOne(lqw);

        //减少数量
        if (cartOne!=null){
            Integer number = cartOne.getNumber();
            cartOne.setNumber(number-1);

            if (cartOne.getNumber()==0){
                shoppingCartDao.deleteById(cartOne);
            }else {
                shoppingCartDao.updateById(cartOne);
            }
        }
        return cartOne;
    }


}
